package com.xquant.databasemanagement.controller.panel.center;

import com.xquant.databasemanagement.bean.SequenceBean;
import com.xquant.databasemanagement.controller.FormController;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.*;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.VBox;

import java.net.URL;
import java.util.ResourceBundle;

public class SequencePanelController implements Initializable, FormController {

    @FXML
    private VBox sequencePanel;
    @FXML
    private TextField sequenceName;

    @FXML
    private TextField sequenceMinValue;

    @FXML
    private TextField sequenceMaxValue;

    @FXML
    private TextField sequenceStep;

    @FXML
    private TextField sequenceStartValue;

    @FXML
    private TextField sequenceCacheSize;

    @FXML
    private CheckBox isCycle;

    private TreeItem treeItem = null;

    private SequenceBean sequenceBean = null;

    private TreeView treeView;

    @Override
    public void initialize(URL location, ResourceBundle resources) {

    }

    @Override
    public void clear(){
        treeItem = null;
        sequenceBean = null;
        sequenceName.clear();
        sequenceMaxValue.clear();
        sequenceStep.clear();
        sequenceStartValue.clear();
        sequenceCacheSize.clear();
        sequenceMinValue.clear();
        isCycle.setSelected(false);
    }

    @Override
    public boolean isValid() {
        if (!sequenceName.getText().matches("^\\w+$")) {
            new Alert(Alert.AlertType.NONE, "表命名不正确", new ButtonType[]{ButtonType.CLOSE}).show();
            return false;
        }else
            return true;
    }


    public void setTreeItem(TreeItem treeItem) {
        this.treeItem = treeItem;
    }




    public void setSequenceBean(SequenceBean sequenceBean) {
        this.sequenceBean = sequenceBean;
        sequenceName.setText(sequenceBean.getName());
        sequenceMaxValue.setText(sequenceBean.getSequenceMaxValue());
        sequenceMinValue.setText(sequenceBean.getSequenceMinValue());
        sequenceCacheSize.setText(sequenceBean.getSequenceCacheSize());
        sequenceStartValue.setText(sequenceBean.getSequenceStartValue());
        sequenceStep.setText(sequenceBean.getSequenceStep());
        isCycle.setSelected(sequenceBean.getCycle());
    }


    public void setSequenceBean( ){
        if (this.sequenceBean == null)
            this.sequenceBean = new SequenceBean();
        this.sequenceBean.setName(sequenceName.getText());
        this.sequenceBean.setSequenceMaxValue(sequenceMaxValue.getText());
        this.sequenceBean.setSequenceMinValue(sequenceMinValue.getText());
        this.sequenceBean.setSequenceStep(sequenceStep.getText());
        this.sequenceBean.setSequenceCacheSize(sequenceCacheSize.getText());
        this.sequenceBean.setSequenceStartValue(sequenceStartValue.getText());
        this.sequenceBean.setCycle(isCycle.isSelected());

    }

    @FXML
    private void actionSaveButton(ActionEvent event) {
        if (isValid()) {
            this.setSequenceBean();
            if (this.treeItem != null) {
                treeItem.getChildren().add(new TreeItem(this.sequenceBean));
            } else
                treeView.refresh();
            clear();
            ((BorderPane) sequencePanel.getParent()).setCenter(null);
        }
    }

    public void setTreeView(TreeView treeView){
        this.treeView = treeView;
    }
}
